﻿using System;
using UnityEngine;

namespace Share
{
    public class ObjectMgr : SingletonObject<ObjectMgr>
    {
        public T CreateObj<T>(string strName) where T : UnityEngine.Object
        {
            strName = strName.Replace("\n", "");
            try
            {

                T obj = Resources.Load<T>(strName);
                return obj;
            }
            catch
            {
                XKLog.LogError($"创建object[{strName}]失败");
                return null;
            }
        }
        public GameObject CreateObj(string strName, Transform parent = null)
        {
            strName = strName.Replace("\n", "");
            try
            {

                var obj = Resources.Load<GameObject>(strName);
                //bool isDontDestory = ResourceTool.IsAssetDontDestory(strName);
                //var obj = ResourceLoader.Instance.LoadAsset<GameObject>(strName, isDontDestory);
                //if (obj == null)
                //{
                //   XKLog.LogWarning(string.Format( "读取profabs失败! 文件{0}不存在!", strName ) );
                //    return null;
                //}

                return GameObject.Instantiate(obj, parent);
            }
            catch
            {
                XKLog.LogError($"创建object[{strName}]失败");
                return null;
            }


        }
        public GameObject CreateObj(GameObject obj, Transform parent = null)
        {
            try
            {
                return GameObject.Instantiate(obj, parent);
            }
            catch
            {
                XKLog.LogWarning(string.Format("创建失败!"));
                return null;
            }

        }
        public GameObject CreateNoDestoryObj(string strName, Transform parent = null)
        {
            var obj = ResourceLoader.Instance.LoadAsset<GameObject>(strName, true);
            if (obj == null)
            {
                XKLog.LogWarning(string.Format("读取profabs失败! 文件{0}不存在!", strName));
                return null;
            }
            return GameObject.Instantiate(obj);
        }

        //提供创建obj 实例化的快捷接口，ResourceLoader资源，然后实例化Instantiate
        public void CreateObjAysnc(string strName, Transform parent = null, Action<GameObject> _callBack = null)
        {
            ResourceLoader.Instance.LoadAssetAsync<GameObject>(strName, (obj) =>
            {
                if (obj != null)
                {
                    if (parent != null && !parent.Equals(null))
                    {
                        var gameObj = GameObject.Instantiate(obj, parent) as GameObject;
                        _callBack?.Invoke(gameObj);
                    }
                    else
                    {
                        var gameObj = GameObject.Instantiate(obj) as GameObject;
                        _callBack?.Invoke(gameObj);
                    }
                }
                else
                {
                    XKLog.LogWarning("创建prbfabs失败 = " + strName);
                    _callBack?.Invoke(null);
                }
            });
        }


        public AudioClip CreateAudioClip(string strName)
        {
            return ResourceLoader.Instance.LoadAsset<AudioClip>(strName);
        }


        //提供创建obj的一个快捷方式
        public GameObject InstantiateObject(GameObject obj, Transform parent = null)
        {
            var gameObj = UnityEngine.Object.Instantiate(obj, parent);
            //gameObj.SetActive(true);
            return gameObj;
        }
    }
}